﻿using System.Reflection;
using Biz.Utility;
using DataLogical;

namespace Biz.Factory
{
    public class Factory
    {
        private static Factory factory = null;
        private static object initLock = new object();

        private Factory() { }

        /// <summary>
        /// #设计模式#单实例
        /// </summary>
        /// <returns></returns>
        public static Factory getInstance()
        {
            if (factory == null)
            {
                lock (initLock)
                {
                    if (factory == null)
                    {
                        factory = new Factory();
                    }
                }
            }
            return factory;
        }

        /// <summary>
        /// #设计模式#工厂模式，反射机制
        /// </summary>
        /// <param name="_type"></param>
        /// <returns></returns>
        public IProduct CreateProduct(string _type)
        {
            if (string.IsNullOrEmpty(_type))
                return null;
            Assembly ass = Assembly.GetExecutingAssembly();
            string className = ClassDefine.getClassName(_type) ;
            if (string.IsNullOrEmpty(className))
                return null;
            IProduct result = (IProduct)ass.CreateInstance(className);
            return result;
        }
    }
}
